package com.zhss.redis.list;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;

import java.util.stream.IntStream;

/**
 * @ClassName SecKillDemo
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2021/9/10 23:04
 * @Version 1.0
 */
public class SecKillDemo {
	Jedis jedis = new Jedis(new HostAndPort("121.4.79.149", 6379));
	private String list_key = "sec_queue";

	/**
	 * 入队
	 * @param secKillRequest
	 */
	public void enqueueSecKillRequest(String secKillRequest) {
		jedis.lpush(list_key, secKillRequest);
	}

	/**
	 * 出队
	 * @return
	 */
	public String dequeueSecKillRequest() {
		String rpop = jedis.rpop(list_key);
		return rpop;
	}

	public static void main(String[] args) {
		SecKillDemo demo = new SecKillDemo();
		IntStream.rangeClosed(0, 10).forEach(i->{
			demo.enqueueSecKillRequest("第"+i+"个秒杀请求");
		});

		while (true) {
			String secKillRequest = demo.dequeueSecKillRequest();
			if ("null".equalsIgnoreCase(secKillRequest) || secKillRequest == null) {
				break;
			}
			System.out.println(secKillRequest);
		}
	}
}
